import '../binary/FieldTypeInterface.dart';
import '../socket/dto/base/Req.dart';
import '../socket/dto/business/PullData.dart';

// 文件上传
class FileInfoReq extends TenantReq implements FieldTypeInterface {
  String userId  = '';
  String fileName ='';
  String fileUrl = '';
  int fileSize = 0;
  String fileServerId = '';
  String fileAuthCode = '';
  String fileMd5 = '';

  FileInfoReq() : super(tenantCode: '', terminalType: TerminalType.android.name);

  @override
  Map<String, FieldWrapper> getFieldTypeMap() {
    var map = <String, FieldWrapper>{};

    var userIdFieldWrapper = FieldWrapper(FieldType.string, (FileInfoReq req)=>req.userId,
            (FileInfoReq req, String? uerId) =>req.userId = (uerId??''));
    map["userId"] = userIdFieldWrapper;

    var fileNameFieldWrapper = FieldWrapper(FieldType.string, (FileInfoReq req)=>req.fileName,
            (FileInfoReq req, String? fileName) =>req.fileName = (fileName??''));
    map["fileName"] = fileNameFieldWrapper;

    var fileUrlFieldWrapper = FieldWrapper(FieldType.string, (FileInfoReq req)=>req.fileUrl,
            (FileInfoReq req, String? fileUrl) =>req.fileUrl = (fileUrl??''));
    map["fileUrl"] = fileUrlFieldWrapper;

    var fileSizeWrapper = FieldWrapper(FieldType.int, (FileInfoReq req)=>req.fileSize,
            (FileInfoReq req, int? fileSize) =>req.fileSize = (fileSize??0));
    map["fileSize"] = fileSizeWrapper;

    var fileServerIdWrapper = FieldWrapper(FieldType.string, (FileInfoReq f)=>f.fileServerId,
            (FileInfoReq req, String? fileServerId)=>req.fileServerId = (fileServerId??''));
    map["fileServerId"] = fileServerIdWrapper;

    var fileAuthCodeWrapper = FieldWrapper(FieldType.string, (FileInfoReq f)=>f.fileAuthCode,
            (FileInfoReq req, String? fileAuthCode)=>req.fileAuthCode = (fileAuthCode??''));
    map["fileAuthCode"] = fileAuthCodeWrapper;

    var fileMd5Wrapper = FieldWrapper(FieldType.string, (FileInfoReq f)=>f.fileMd5,
            (FileInfoReq req, String? fileMd5)=>req.fileMd5 = (fileMd5??''));
    map["fileMd5"] = fileMd5Wrapper;

    var tenantCodeWrapper = FieldWrapper(FieldType.string, (FileInfoReq f)=>f.tenantCode,
            (FileInfoReq req, String? tenantCode)=>req.tenantCode = (tenantCode??''));
    map["tenantCode"] = tenantCodeWrapper;

    var terminalTypeWrapper = FieldWrapper(FieldType.string, (FileInfoReq f)=>f.terminalType,
            (FileInfoReq req, String? terminalType)=>req.terminalType = (terminalType??''));
    map["terminalType"] = terminalTypeWrapper;

    var createDateTimeWrapper = FieldWrapper(FieldType.date, (FileInfoReq f)=>f.createDateTime,
            (FileInfoReq req, DateTime? terminalType)=>req.createDateTime = (createDateTime??DateTime.now()));
    map["createDateTime"] = createDateTimeWrapper;

    var localSeqWrapper = FieldWrapper(FieldType.string, (FileInfoReq f)=>f.localSeq,
            (FileInfoReq req, String? localSeq)=>req.localSeq = (localSeq??''));
    map["localSeq"] = localSeqWrapper;
    return map;
  }
}

class FileDownloadReq extends TenantReq implements FieldTypeInterface {
  String userId = '';
  String fileUrl = '';
  String fileServerId = '';
  String fileAuthCode = '';
  String localDir = '';

  FileDownloadReq() : super(tenantCode: '', terminalType: TerminalType.android.name);

  @override
  Map<String, FieldWrapper> getFieldTypeMap() {
    var map = <String, FieldWrapper>{};

    var userIdFieldWrapper = FieldWrapper(FieldType.string, (FileDownloadReq req)=>req.userId,
            (FileDownloadReq req, String? uerId) =>req.userId = (uerId??''));
    map["userId"] = userIdFieldWrapper;

    var fileUrlFieldWrapper = FieldWrapper(FieldType.string, (FileDownloadReq req)=>req.fileUrl,
            (FileDownloadReq req, String? fileUrl) =>req.fileUrl = (fileUrl??''));
    map["fileUrl"] = fileUrlFieldWrapper;

    var fileServerIdWrapper = FieldWrapper(FieldType.string, (FileDownloadReq f)=>f.fileServerId,
            (FileDownloadReq req, String? fileServerId)=>req.fileServerId = (fileServerId??''));
    map["fileServerId"] = fileServerIdWrapper;

    var fileAuthCodeWrapper = FieldWrapper(FieldType.string, (FileDownloadReq f)=>f.fileAuthCode,
            (FileDownloadReq req, String? fileAuthCode)=>req.fileAuthCode = (fileAuthCode??''));
    map["fileAuthCode"] = fileAuthCodeWrapper;

    var localDirWrapper = FieldWrapper(FieldType.string, (FileDownloadReq f)=>f.localDir,
            (FileDownloadReq req, String? localDir)=>req.localDir = (localDir??''));
    map["localDir"] = localDirWrapper;

    var tenantCodeWrapper = FieldWrapper(FieldType.string, (FileDownloadReq f)=>f.tenantCode,
            (FileDownloadReq req, String? tenantCode)=>req.tenantCode = (tenantCode??''));
    map["tenantCode"] = tenantCodeWrapper;

    var terminalTypeWrapper = FieldWrapper(FieldType.string, (FileDownloadReq f)=>f.terminalType,
            (FileDownloadReq req, String? terminalType)=>req.terminalType = (terminalType??''));
    map["terminalType"] = terminalTypeWrapper;

    var createDateTimeWrapper = FieldWrapper(FieldType.date, (FileDownloadReq f)=>f.createDateTime,
            (FileDownloadReq req, DateTime? terminalType)=>req.createDateTime = (createDateTime??DateTime.now()));
    map["createDateTime"] = createDateTimeWrapper;

    var localSeqWrapper = FieldWrapper(FieldType.string, (FileDownloadReq f)=>f.localSeq,
            (FileDownloadReq req, String? localSeq)=>req.localSeq = (localSeq??''));
    map["localSeq"] = localSeqWrapper;
    return map;
  }
}



class FileDownloadInfo implements FieldTypeInterface {

  int fileSize = 0;
  String md5 = '';

  FileDownloadInfo();

  @override
  Map<String, FieldWrapper> getFieldTypeMap() {
    var map = <String, FieldWrapper>{};

    var fileSizeWrapper = FieldWrapper(FieldType.long, (FileDownloadInfo req)=>req.fileSize,
            (FileDownloadInfo req, int? fileSize) =>req.fileSize = (fileSize??0));
    map["fileSize"] = fileSizeWrapper;

    var userIdFieldWrapper = FieldWrapper(FieldType.string, (FileDownloadInfo req) => req.md5,
            (FileDownloadInfo req, String? md5) => req.md5 = (md5 ?? ''));
    map["md5"] = userIdFieldWrapper;

    return map;
  }
}

class FileUploadReport implements FieldTypeInterface {
  int uploadSize = 0;

  @override
  Map<String, FieldWrapper> getFieldTypeMap() {
    var map = <String, FieldWrapper>{};

    var fileSizeWrapper = FieldWrapper(FieldType.long, (FileUploadReport req)=>req.uploadSize,
            (FileUploadReport req, int? uploadSize) =>req.uploadSize = (uploadSize??0));
    map["uploadSize"] = fileSizeWrapper;

    return map;
  }

}

enum FileExistedType {
  nodownload,            // 文件未下载
  downloaded,            // 文件已经下载成功，请不要重复下载
  downloadInCache,       // 文件已经下载到缓存目录中，请完成后续流程
  downloadInUserDir,     // 文件已经下载到用户目录中，请完成后续流程
}

class FileUploadData {
  String tenantCode;
  String userId;
  int messageId;
  String messageServerId;
  String fileOriginName;
  String filePath;
  String ip;
  int port;

  FileUploadData(this.tenantCode, this.userId, this.messageId, this.messageServerId,
      this.fileOriginName, this.filePath, this.ip, this.port);
}

class FileUploadResult {
  int fileSize;
  int fileUploadSize;
  bool isSuccess;
  String errorMsg;

  FileUploadResult(this.fileSize, this.fileUploadSize, this.isSuccess, this.errorMsg);
}


class FileDownloadData {
  String tenantCode;
  String userId;
  int messageId;
  String messageServerId;
  int fileSize;
  String fileOriginName;
  String md5;
  String fileUrl;
  String cacheDir;
  String userDir;
  String ip;
  int port;

  FileDownloadData(this.tenantCode, this.userId, this.messageId, this.messageServerId,
      this.fileOriginName, this.fileSize, this.md5, this.fileUrl, this.cacheDir, this.userDir,
      this.ip, this.port);
}

class FileDownloadResult implements ErrorResult {
  int fileSize;              // 文件总大小
  int fileDownloadSize;      // 已下载大小
  bool isSuccess;            // 是否下载成功
  String errorMsg;           // 异常信息

  FileDownloadResult(this.fileSize, this.fileDownloadSize, this.isSuccess, this.errorMsg);
}

class ErrorResult {

  String errorMsg;

  ErrorResult(this.errorMsg);
}